<?php

namespace App\Console\Commands;

use App\Models\Flow;
use App\Models\Order;
use App\Models\Rebate;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class OrderRebate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:rebate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单执行分润';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //对支付成功的订单执行分润
        try {
            DB::beginTransaction();
            $orders = Order::with(['product','user'])->where('rebate_status','0')
                            ->where('status','<>',NO_PAY)
                            ->orderBy('id','asc')->limit(100)->get();
            if ($orders) {
                foreach ($orders as $order) {
                    //订单的商品快照中获取上级、上上级分润比例
                    $snapshoot = json_decode($order->snapshoot,1);
                    //设置了上级分润时并且有上级时
                    if ($snapshoot['p_rate'] > 0 && $order['user']['pid'] >0) {
                        //上级分润，换算成元。
                        $p_money = $order['total_fee'] * 0.01 * $snapshoot['p_rate'] * 0.01;
                        //上级分润
                        User::where('id',$order['user']['pid'])->increment('total_money',$p_money);
                        User::where('id',$order['user']['pid'])->increment('current_money',$p_money);

                        //插入分润记录
                        $rebate = Rebate::create([
                            'money'     =>  $p_money,
                            'user_id'   =>  $order['user']['pid'],
                            'order_id'  =>  $order['id'],
                            'level'     =>  FIRST
                        ]);
                        //插入流水记录
                        Flow::create([
                            'money'         =>  $p_money,
                            'type'          =>  REBATE,
                            'user_id'       =>  $order['user']['pid'],
                            'business_id'   =>  $rebate['id']
                        ]);
                    }
                    //上上级分润
                    if ($snapshoot['p_p_rate'] > 0) {
                        $parent = User::find($order['user']['pid']);
                        if ($parent['pid'] > 0) {
                            $p_p_money = $order['total_fee'] * 0.01 * $snapshoot['p_p_rate'] * 0.01;
                            //上级分润
                            User::where('id',$parent['pid'])->increment('total_money',$p_p_money);
                            User::where('id',$parent['pid'])->increment('current_money',$p_p_money);

                            //插入分润记录
                            $rebate = Rebate::create([
                                'money'     =>  $p_p_money,
                                'user_id'   =>  $parent['pid'],
                                'order_id'  =>  $order['id'],
                                'level'     =>  SECOND
                            ]);
                            //插入流水记录
                            Flow::create([
                                'money'         =>  $p_p_money,
                                'type'          =>  REBATE,
                                'user_id'       =>  $parent['pid'],
                                'business_id'   =>  $rebate['id']
                            ]);
                        }
                    }
                    Order::where('id',$order['id'])->update(['rebate_status' => '1']);
                }
            }
            DB::commit();
        }catch (\Exception $exception) {
            Log::info("分润失败");
            Log::info($exception->getMessage());
            DB::rollBack();
        }
    }
}
